clear all 
set more off 
set maxvar 15000 
clear matrix


**********************************************************************************************************************************************
**********************************************************************************************************************************************
**** IGE VS RANK OVER TIME: MISSING PARENTAL INCOME
**********************************************************************************************************************************************
**********************************************************************************************************************************************
        
	use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 

    global ige_list "1 2 3 4"
    global rank_list "1 2 3 4 5"

    foreach var in ige rank  {
    preserve
    estimates clear 

        forval i=1(1)5 {
            gen est_`i'=. 
            gen est_lb_`i' =.
            gen est_ub_`i' =.
        }
        
        foreach fix in $`var'_list {

        * Dependent variables
            if "`var'"=="ige" local dep "log_son_baseline"
            
            if "`var'"=="rank" & "`fix'"=="1" local dep "rank_son_baseline"
            if "`var'"=="rank" & "`fix'"=="2" local dep "rank_son_father_notworking"
            if "`var'"=="rank" & "`fix'"=="3" local dep "rank_son_parent"
            if "`var'"=="rank" & "`fix'"=="4" local dep "rank_son_parent_all"
            if "`var'"=="rank" & "`fix'"=="5" local dep "rank_son_zeros_parent"
            
        * Independent variables
            if "`var'"=="ige" & "`fix'"=="1" local indep "log_father_baseline"
            if "`var'"=="ige" & "`fix'"=="2" local indep "father_baseline_plus_notworking" 
            if "`var'"=="ige" & "`fix'"=="3" local indep "parent_income" 
            if "`var'"=="ige" & "`fix'"=="4" local indep "parent_income_all" 

            if "`var'"=="rank" & "`fix'"=="1" local indep "rank_father_baseline"
            if "`var'"=="rank" & "`fix'"=="2" local indep "rank_father_notworking"
            if "`var'"=="rank" & "`fix'"=="3" local indep "rank_parent" 
            if "`var'"=="rank" & "`fix'"=="4" local indep "rank_parent_all" 
            if "`var'"=="rank" & "`fix'"=="5" local indep "rank_parent_zeros" 
            
            
        * Weights
            if "`fix'"=="1" local weight "wgt_sex_race"
            if "`fix'"=="2" local weight "wgt_sex_race_father_notworking"
            if "`fix'"=="3" local weight "wgt_sex_race_parent"
            if "`fix'"=="4" local weight "wgt_sex_race_parent_all"
            if "`fix'"=="5" local weight "wgt_sex_race_zeros"
            
        * Sample
            if "`fix'"=="1" local cond2 "& baseline_sample==1"
            if "`fix'"=="2" local cond2 "& father_notworking_sample==1"
            if "`fix'"=="3" local cond2 "& parent_sample==1"
            if "`fix'"=="4" local cond2 "& parent_all_sample==1"
            if "`fix'"=="5" local cond2 "& zeros_sample==1"
            
            levelsof decade, local(decades)
            
        *1. 
            foreach x of local decades {
            eststo : reg `dep' `indep'  if decade==`x' `cond2' [pw=`weight'], robust 
                replace est_`fix' = _b[`indep'] if decade==`x'
                replace est_ub_`fix' = _b[`indep']+1.96*_se[`indep'] if decade==`x'
                replace est_lb_`fix' = _b[`indep']-1.96*_se[`indep'] if decade==`x'
            }
        
        }
        
    * Figures
        bysort decade: keep if _n==1
        keep decade est_*
        
        reshape long est_ est_lb_ est_ub_, i(decade) j(estimate)
        replace decade= decade+1 if estimate==2 
        replace decade= decade+2 if estimate==3
        replace decade= decade+3 if estimate==4
        replace decade= decade+4 if estimate==5

        
    if "`var'"=="ige" {
        local title "IGE"
        local ub "1"
        local lb "0"
        local skip "0.25"
        local maincolor "midblue"
        local color2 "navy"
        local color3 "green"
        local color4 "orange*0.5"
        local color5 "pink*0.5"
    }

    if "`var'"=="rank" {
        local title "Rank"
        local ub "0.45"
        local lb "0.15"
        local skip "0.1"
        local maincolor "orange"
        local color2 "cranberry"
        local color3 "pink*0.75"
        local color4 "lavender"
        local color5 "gray"
        local color6 "black*0.75"
    }
        
    if "`var'"=="ige" {
        #delimit ;
        twoway (scatter est_ decade if estimate==1,  msymbol(circle) mcolor(`maincolor') msize(medium) ) 
               (rcap est_lb_  est_ub_  decade if estimate==1, lpatter(solid) lcolor(`maincolor') lwidth(0.5))
                
               (scatter est_ decade if estimate==2,  msymbol(diamond) mcolor(`color2') msize(small) ) 
               (rcap est_lb_  est_ub_  decade if estimate==2, lpatter(solid) lcolor(`color2') lwidth(0.5) )
               
                (scatter est_ decade if estimate==3,  msymbol(triangle_hollow) mcolor(`color3') msize(small) ) 
               (rcap est_lb_  est_ub_  decade if estimate==3, lpatter(solid) lcolor(`color3') lwidth(0.5) )
               
                (scatter est_ decade if estimate==4,  msymbol(square) mcolor(`color4') msize(small) ) 
               (rcap est_lb_  est_ub_  decade if estimate==4, lpatter(solid) lcolor(`color4') lwidth(0.5) ) 
               ,
               
        xti(" " "Decade of respondent's birth") xlabel(1910(10)1970) xscale(range(1905 1975))
        ylabel(`lb'(`skip')`ub', axis(1)) yti("`title' coefficient" " ", axis(1)) 
        legend(on ring(0) pos(8) rows(4) order(1 "Baseline" 3 "+ Non-working fathers" 5 "+ Working mothers" 7 "+ Both" ))
        xlabel(1910 "1910s" 1920 "1920s" 1930 "1930s" 1940 "1940s" 1950 "1950s" 1960 "1960s" 1970 "1970s", labsize(small) ) ;  
        #delimit cr
        graph export "$Mydirectory2/appendix_b/`title'_overtime_missings.pdf", as(pdf) replace
    }
        
    if "`var'"=="rank" {
        #delimit ;
        twoway (scatter est_ decade if estimate==1,  msymbol(circle) mcolor(`maincolor') msize(medium) ) 
               (rcap est_lb_  est_ub_  decade if estimate==1, lpatter(solid) lcolor(`maincolor') lwidth(0.5))
               
               (scatter est_ decade if estimate==2,  msymbol(diamond) mcolor(`color2') msize(small) ) 
               (rcap est_lb_  est_ub_  decade if estimate==2, lpatter(solid) lcolor(`color2') lwidth(0.5))
               
               (scatter est_ decade if estimate==3,  msymbol(triangle_hollow) mcolor(`color3') msize(small)) 
               (rcap est_lb_  est_ub_  decade if estimate==3, lpatter(solid) lcolor(`color3') lwidth(0.5))
               
                (scatter est_ decade if estimate==4,  msymbol(square) mcolor(`color4') msize(small)) 
               (rcap est_lb_  est_ub_  decade if estimate==4, lpatter(solid) lcolor(`color4') lwidth(0.5))
               
                (scatter est_ decade if estimate==5,  msymbol(triangle) mcolor(`color5') msize(small) ) 
               (rcap est_lb_  est_ub_  decade if estimate==5, lpatter(solid) lcolor(`color5') lwidth(0.5) ) 
               ,
               
        xti(" " "Decade of respondent's birth") xlabel(1910(10)1970) xscale(range(1905 1975))
        ylabel(`lb'(`skip')`ub', axis(1)) yti("`title' coefficient" " ", axis(1)) 
        legend(on ring(0) pos(2) rows(5) order(1 "Baseline" 3 "+ Non-working fathers" 5 "+ Working mothers" 7 "+ Both" 9 "+ No parental info. (lowest rank)" ))
        xlabel(1910 "1910s" 1920 "1920s" 1930 "1930s" 1940 "1940s" 1950 "1950s" 1960 "1960s" 1970 "1970s", labsize(small) ) ;  
        #delimit cr
        graph export "$Mydirectory2/appendix_b/`title'_overtime_missings.pdf", as(pdf) replace
    }
        
    restore

    }
